package com.stylefeng.guns.rest.common.interceptor;

import com.stylefeng.guns.biz.acct.LoginBizImpl;
import com.stylefeng.guns.common.bo.AuthUserBo;
import com.stylefeng.guns.common.bo.WebuserBo;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.dao.WebuserMapper;
import com.stylefeng.guns.common.persistence.model.Webuser;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.support.RestConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;


/**
 * @author carloszhang
 */
@Slf4j
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Value("${spring.needLogin}")
    private boolean needLogin;


    @Autowired
    private WebuserMapper webuserMapper;//查询用户基本信息

    @Autowired
    private LoginBizImpl loginBizImpl;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        log.info("用户登录url:{}", request.getRequestURI());
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = (String) headerNames.nextElement();
            log.info("头信息header:{},value:{}", name,request.getHeader(name));
        }

        String accessToken = request.getHeader(RestConstant.ACCESS_TOKEN);
        log.info("用户登录url:{},token:{}", request.getRequestURI(), accessToken);
        if (StringUtils.isBlank(accessToken)) {
            throw new GunsException(BizExceptionEnum.USER_NOT_LOGIN);//用户未登录
        }
        String accessSource = request.getHeader(RestConstant.ACCESS_SOURCE);
        if (StringUtils.isBlank(accessSource)) {
            throw new GunsException(BizExceptionEnum.USER_ACCESS_ERROR);//用户未登录
        }
        AuthUserBo authUserBo = loginBizImpl.getCacheUser(accessToken);
        if (authUserBo != null && authUserBo.getId() != 0L) {//如果用户存在
            WebuserBo webuserBo = getWebUser(authUserBo.getId());
            response.setHeader(RestConstant.ACCESS_TOKEN, accessToken);
            response.setHeader(RestConstant.ACCESS_HEADERS, RestConstant.ACCESS_TOKEN);
            request.setAttribute(RestConstant.USER_INFO_KEY, webuserBo);
            return true;
        } else {
            throw new GunsException(BizExceptionEnum.USER_NOT_LOGIN);//用户未登录
        }
    }


    private WebuserBo getWebUser(long userId) {
        Webuser webuser = webuserMapper.selectById(userId);
        if (webuser == null || StringUtils.isBlank(webuser.getTelno())) {
            throw new GunsException(BizExceptionEnum.USER_NOT_REGISTER);//用户未注册
        }
        WebuserBo webuserBo = new WebuserBo();
        BeanUtils.copyProperties(webuser, webuserBo);
        return webuserBo;
    }
}
